package ar.edu.unsam.algo3.plantasvszombies.wicket;

import ar.edu.unsam.algo3.plantasvszombies.dominio.Fila;
import ar.edu.unsam.algo3.plantasvszombies.dominio.Jardin;
import ar.edu.unsam.algo3.plantasvszombies.dominio.JardinZen;
import ar.edu.unsam.algo3.plantasvszombies.dominio.Planta;
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.JardinAppModel;
import ar.edu.unsam.algo3.plantasvszombies.wicket.BatallaPanel;
import ar.edu.unsam.algo3.plantasvszombies.wicket.FilaPanel;
import ar.edu.unsam.algo3.plantasvszombies.wicket.MainPage;
import java.util.List;
import java.util.Stack;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function0;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure0;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.uqbar.commons.model.UserException;
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods;
import org.uqbar.wicket.xtend.XAjaxLink;
import org.uqbar.wicket.xtend.XLink;
import org.uqbar.wicket.xtend.XListView;

@SuppressWarnings("all")
public class JardinPage extends MainPage {
  @Extension
  private WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new Function0<WicketExtensionFactoryMethods>() {
    public WicketExtensionFactoryMethods apply() {
      WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new WicketExtensionFactoryMethods();
      return _wicketExtensionFactoryMethods;
    }
  }.apply();
  
  private JardinAppModel _jardinApp = new Function0<JardinAppModel>() {
    public JardinAppModel apply() {
      JardinAppModel _jardinAppModel = new JardinAppModel();
      return _jardinAppModel;
    }
  }.apply();
  
  public JardinAppModel getJardinApp() {
    return this._jardinApp;
  }
  
  public void setJardinApp(final JardinAppModel jardinApp) {
    this._jardinApp = jardinApp;
  }
  
  private FilaPanel _pnlFila;
  
  public FilaPanel getPnlFila() {
    return this._pnlFila;
  }
  
  public void setPnlFila(final FilaPanel pnlFila) {
    this._pnlFila = pnlFila;
  }
  
  public JardinPage(final JardinAppModel unJardinApp) {
    this.setJardinApp(unJardinApp);
    JardinAppModel _jardinApp = this.getJardinApp();
    CompoundPropertyModel<JardinAppModel> _asCompoundModel = this._wicketExtensionFactoryMethods.<JardinAppModel>asCompoundModel(_jardinApp);
    this.setDefaultModel(_asCompoundModel);
    Form<JardinAppModel> _form = new Form<JardinAppModel>("tableroForm");
    final Form<JardinAppModel> jardinForm = _form;
    JardinAppModel _jardinApp_1 = this.getJardinApp();
    CompoundPropertyModel<JardinAppModel> _asCompoundModel_1 = this._wicketExtensionFactoryMethods.<JardinAppModel>asCompoundModel(_jardinApp_1);
    jardinForm.setDefaultModel(_asCompoundModel_1);
    Panel _menuNavegacion = this.menuNavegacion();
    this._wicketExtensionFactoryMethods.addChild(this, _menuNavegacion);
    this.agregarDatosJuego(jardinForm);
    this.agregarTablaFilas(jardinForm);
    this.listaJardinesZen(jardinForm);
    this._wicketExtensionFactoryMethods.addChild(this, jardinForm);
  }
  
  public MarkupContainer agregarDatosJuego(final Form<JardinAppModel> form) {
    MarkupContainer _xblockexpression = null;
    {
      JardinAppModel _jardinApp = this.getJardinApp();
      Jardin _jardin = _jardinApp.getJardin();
      String _jugador = _jardin.getJugador();
      Label _label = new Label("jugador", _jugador);
      this._wicketExtensionFactoryMethods.addChild(form, _label);
      PropertyModel<Object> _propertyModel = new PropertyModel<Object>(this, "jardinApp.jardin.recursos");
      Label _label_1 = new Label("recursos", _propertyModel);
      this._wicketExtensionFactoryMethods.addChild(form, _label_1);
      PropertyModel<Object> _propertyModel_1 = new PropertyModel<Object>(this, "jardinApp.jardin.filasDisponibles");
      Label _label_2 = new Label("lugaresDisponibles", _propertyModel_1);
      this._wicketExtensionFactoryMethods.addChild(form, _label_2);
      FeedbackPanel _feedbackPanel = new FeedbackPanel("feedbackPanel");
      final FeedbackPanel feedbackPanel = _feedbackPanel;
      this._wicketExtensionFactoryMethods.addChild(form, feedbackPanel);
      Fila _fila = new Fila();
      FilaPanel _filaPanel = new FilaPanel("pnlFila", _fila);
      this.setPnlFila(_filaPanel);
      FilaPanel _pnlFila = this.getPnlFila();
      MarkupContainer _addChild = this._wicketExtensionFactoryMethods.addChild(form, _pnlFila);
      _xblockexpression = (_addChild);
    }
    return _xblockexpression;
  }
  
  public MarkupContainer listaJardinesZen(final Form<JardinAppModel> form) {
    MarkupContainer _xblockexpression = null;
    {
      JardinAppModel _jardinApp = this.getJardinApp();
      Jardin _jardin = _jardinApp.getJardin();
      List<JardinZen> _tiposPosibles = _jardin.getTiposPosibles();
      XListView<JardinZen> _xListView = new XListView<JardinZen>("jardinesZen", _tiposPosibles);
      final XListView<JardinZen> listView = _xListView;
      final Procedure1<ListItem<JardinZen>> _function = new Procedure1<ListItem<JardinZen>>() {
        public void apply(final ListItem<JardinZen> item) {
          JardinZen _modelObject = item.getModelObject();
          CompoundPropertyModel<JardinZen> _asCompoundModel = JardinPage.this._wicketExtensionFactoryMethods.<JardinZen>asCompoundModel(_modelObject);
          item.setModel(_asCompoundModel);
          XLink<Object> _xLink = new XLink<Object>("btnNvaFila");
          final Procedure1<XLink<Object>> _function = new Procedure1<XLink<Object>>() {
            public void apply(final XLink<Object> it) {
              final Procedure0 _function = new Procedure0() {
                public void apply() {
                  try {
                    JardinZen _modelObject = item.getModelObject();
                    JardinPage.this.agregarFila(_modelObject);
                  } catch (final Throwable _t) {
                    if (_t instanceof UserException) {
                      final UserException e = (UserException)_t;
                      String _message = e.getMessage();
                      it.error(_message);
                    } else {
                      throw Exceptions.sneakyThrow(_t);
                    }
                  }
                }
              };
              it.setOnClick(_function);
            }
          };
          final XLink<Object> miBoton = ObjectExtensions.<XLink<Object>>operator_doubleArrow(_xLink, _function);
          Label _label = new Label("tipo");
          JardinPage.this._wicketExtensionFactoryMethods.addChild(miBoton, _label);
          JardinPage.this._wicketExtensionFactoryMethods.addChild(item, miBoton);
        }
      };
      listView.setPopulateItem(_function);
      MarkupContainer _addChild = this._wicketExtensionFactoryMethods.addChild(form, listView);
      _xblockexpression = (_addChild);
    }
    return _xblockexpression;
  }
  
  public void agregarFila(final JardinZen unZen) {
    Fila _fila = new Fila();
    final Fila fila = _fila;
    fila.setJardinZen(unZen);
    JardinAppModel _jardinApp = this.getJardinApp();
    Jardin _jardin = _jardinApp.getJardin();
    _jardin.agregarFila(fila);
  }
  
  public MarkupContainer agregarTablaFilas(final Form<JardinAppModel> form) {
    MarkupContainer _xblockexpression = null;
    {
      XListView<Fila> _xListView = new XListView<Fila>("misFilas");
      final XListView<Fila> listView = _xListView;
      final Procedure1<ListItem<Fila>> _function = new Procedure1<ListItem<Fila>>() {
        public void apply(final ListItem<Fila> item) {
          Fila _modelObject = item.getModelObject();
          CompoundPropertyModel<Fila> _asCompoundModel = JardinPage.this._wicketExtensionFactoryMethods.<Fila>asCompoundModel(_modelObject);
          item.setModel(_asCompoundModel);
          Label _label = new Label("tipo");
          JardinPage.this._wicketExtensionFactoryMethods.addChild(item, _label);
          Label _label_1 = new Label("plantas");
          JardinPage.this._wicketExtensionFactoryMethods.addChild(item, _label_1);
          XLink<Object> _xLink = new XLink<Object>("eliminar");
          final Procedure1<XLink<Object>> _function = new Procedure1<XLink<Object>>() {
            public void apply(final XLink<Object> it) {
              final Procedure0 _function = new Procedure0() {
                public void apply() {
                  JardinAppModel _jardinApp = JardinPage.this.getJardinApp();
                  Jardin _jardin = _jardinApp.getJardin();
                  Fila _modelObject = item.getModelObject();
                  _jardin.eliminarFila(_modelObject);
                }
              };
              it.setOnClick(_function);
            }
          };
          XLink<Object> _doubleArrow = ObjectExtensions.<XLink<Object>>operator_doubleArrow(_xLink, _function);
          JardinPage.this._wicketExtensionFactoryMethods.addChild(item, _doubleArrow);
          XAjaxLink<Object> _xAjaxLink = new XAjaxLink<Object>("editar");
          final Procedure1<XAjaxLink<Object>> _function_1 = new Procedure1<XAjaxLink<Object>>() {
            public void apply(final XAjaxLink<Object> it) {
              final Procedure1<AjaxRequestTarget> _function = new Procedure1<AjaxRequestTarget>() {
                public void apply(final AjaxRequestTarget target) {
                  Fila _modelObject = item.getModelObject();
                  FilaPanel _filaPanel = new FilaPanel("pnlFila", _modelObject);
                  form.replace(_filaPanel);
                  target.add(form);
                  target.appendJavaScript("$(\'#myModal\').modal(\'show\');");
                }
              };
              it.setOnClick(_function);
            }
          };
          XAjaxLink<Object> _doubleArrow_1 = ObjectExtensions.<XAjaxLink<Object>>operator_doubleArrow(_xAjaxLink, _function_1);
          JardinPage.this._wicketExtensionFactoryMethods.addChild(item, _doubleArrow_1);
          XAjaxLink<Object> _xAjaxLink_1 = new XAjaxLink<Object>("atacar");
          final Procedure1<XAjaxLink<Object>> _function_2 = new Procedure1<XAjaxLink<Object>>() {
            public void apply(final XAjaxLink<Object> it) {
              final Procedure1<AjaxRequestTarget> _function = new Procedure1<AjaxRequestTarget>() {
                public void apply(final AjaxRequestTarget target) {
                  Fila _modelObject = item.getModelObject();
                  Stack<Planta> _plantas = _modelObject.getPlantas();
                  boolean _empty = _plantas.empty();
                  boolean _not = (!_empty);
                  if (_not) {
                    Fila _modelObject_1 = item.getModelObject();
                    BatallaPanel _batallaPanel = new BatallaPanel("pnlFila", _modelObject_1);
                    form.replace(_batallaPanel);
                    target.add(form);
                    String _alCerrarModal = JardinPage.this.alCerrarModal();
                    target.appendJavaScript(_alCerrarModal);
                  } else {
                    it.error("No se puede atacar una fila vacia");
                    target.add(form);
                  }
                }
              };
              it.setOnClick(_function);
            }
          };
          XAjaxLink<Object> _doubleArrow_2 = ObjectExtensions.<XAjaxLink<Object>>operator_doubleArrow(_xAjaxLink_1, _function_2);
          JardinPage.this._wicketExtensionFactoryMethods.addChild(item, _doubleArrow_2);
        }
      };
      listView.setPopulateItem(_function);
      MarkupContainer _addChild = this._wicketExtensionFactoryMethods.addChild(form, listView);
      _xblockexpression = (_addChild);
    }
    return _xblockexpression;
  }
  
  public String alCerrarModal() {
    return "$(\'#myModal\').modal(\'show\');";
  }
}
